<!-- PromotionList.vue -->

<template>
  <div class="danye">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>活动管理</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="mar-top">
      <el-button
        style="margin-right: 1rem"
        type="primary"
        @click="showAddDialog"
        >添加活动酒品</el-button
      >
      <el-input
        v-model="searchKeyword"
        placeholder="请输入搜索活动名称"
        style="width: 200px; margin-bottom: 10px"
        clearable
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="handleSearch"
        ></el-button>
      </el-input>
    </div>
    <el-table :data="promotionList" stripe>
      <el-table-column prop="promotionId" label="ID"></el-table-column>
      <el-table-column prop="name" label="活动名称"></el-table-column>
      <el-table-column prop="startDate" label="开始日期"></el-table-column>
      <el-table-column prop="endDate" label="结束日期"></el-table-column>
      <el-table-column
        prop="discountPercent"
        label="折扣"
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    />
    <dialogAdd ref="dialogAdd" :dialog-title="dialogTitle"></dialogAdd>
  </div>
</template>

<script>
import dialogAdd from "@/components/dialogAdd.vue";
import { Message } from "element-ui";
import {
  activelist,
  delActive,
} from "@/api/promotion";
export default {
  components: {
    dialogAdd,
  },
  data() {
    return {
      current: 1,
      size: 10,
      total: 0,
      searchKeyword: "",
      promotionList: [],
      dialogVisible: false,
      dialogTitle: "",
    };
  },
  created() {
    this.getPromotion();
  },
  methods: {
    handleSizeChange(val) {
      this.current = val;
    },
    handleCurrentChange(val) {
      this.size = val;
    },
    handleEdit(row) {
      this.dialogTitle = "edit";
      console.log(this.dialogTitle);
      this.$refs.dialogAdd.dialogVisible = true;
      this.$refs.dialogAdd.formData = row;
    },
    showAddDialog() {
      this.dialogTitle = "add";
      this.$refs.dialogAdd.dialogVisible = true;
      this.$refs.dialogAdd.formData={}
    },
   async handleDelete(row) {
      const res = await delActive(row.promotionId);
      if (res.code == 200) {
       Message(res.message);
      } else {
        Message(res.message);
      }
 
    },
    handleSearch() {
     this.getPromotion(this.searchKeyword)
    },
    async getPromotion(searchKeyword) {
      const res = await activelist(searchKeyword);
      if (res.code == 200) {
        this.promotionList = res.data.list.records;
        this.size = res.data.list.size;
        this.total = res.data.list.total;
        this.current = res.data.list.current;
      } else {
        Message(res.message);
      }
    },
  },
};
</script>
